package com.yenlien.traceyun.breed.service;

import com.yenlien.traceyun.breed.dao.BreedInputsDao;
import com.yenlien.traceyun.breed.dao.BreedInputsLogDao;
import com.yenlien.traceyun.breed.entity.BreedInputs;
import com.yenlien.traceyun.breed.entity.BreedInputsLog;
import com.yenlien.traceyun.business.dao.SupplierLogDao;
import com.yenlien.traceyun.business.entity.SupplierLog;
import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import com.yenlien.traceyun.common.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/9/22.
 */
@Service
public class BreedInputsService {
    @Autowired
    private BreedInputsDao breedInputsDao;
    @Autowired
    private FileRestService fileRestService;
    @Autowired
    private BreedInputsLogDao breedInputsLogDao;
    @Autowired
    private SupplierLogDao supplierLogDao;

    public Page<BreedInputs> findByList(final BreedInputs breedInputs) {
        Pageable pageable = PageRequest.of(breedInputs.getPage(), breedInputs.getPageSize(), Sort.Direction.DESC, "createtime");
        return breedInputsDao.findAll(new Specification<BreedInputs>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<BreedInputs> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //构造查询条件
                List<Predicate> list_and = new ArrayList<>();
                List<Predicate> list_or = new ArrayList<>();
                if (!StringUtils.isEmpty(breedInputs.getCompanyId())) {
                    list_and.add(cb.equal(root.get("companyId"), breedInputs.getCompanyId()));
                }
                Predicate[] predicate = new Predicate[list_and.size()];
                Predicate pre_and = cb.and(list_and.toArray(predicate));
                if (!StringUtils.isEmpty(breedInputs.getQuery())) {
                    list_or.add(cb.like(root.get("inputsName"), "%" + breedInputs.getQuery() + "%"));
                    list_or.add(cb.like(root.get("batch"), "%" + breedInputs.getQuery() + "%"));
                    list_or.add(cb.like(root.get("supplyName"), "%" + breedInputs.getQuery() + "%"));
                    list_or.add(cb.like(root.get("type"), "%" + breedInputs.getQuery() + "%"));
                } else {
                    return pre_and;
                }
                Predicate[] or = new Predicate[list_or.size()];
                Predicate pre_or = cb.or(list_or.toArray(or));
                return query.where(pre_and, pre_or).getRestriction();
            }
        }, pageable);
    }

    public Page<BreedInputsLog> findByLogList(final BreedInputsLog breedInputsLog) {
        Pageable pageable = PageRequest.of(breedInputsLog.getPage(), breedInputsLog.getPageSize(), Sort.Direction.DESC, "time");
        return breedInputsLogDao.findAll(new Specification<BreedInputsLog>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<BreedInputsLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> inputsId = root.get("inputsId");
                //构造查询条件
                List<Predicate> list = new ArrayList<>();
                if (!StringUtils.isEmpty(breedInputsLog.getInputsId())) {
                    list.add(cb.equal(inputsId, breedInputsLog.getInputsId()));
                }
                if (!StringUtils.isEmpty(breedInputsLog.getUserName())) {
                    list.add(cb.like(root.get("userName"), "%" + breedInputsLog.getUserName() + "%"));
                }
                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }
        }, pageable);
    }

    public void save(BreedInputs breedInputs, List<MultipartFile> files) throws Exception {
        if (files.size() > 0) {
            if (StringUtils.isEmpty(breedInputs.getId())) {
                fileRestService.upload(null, breedInputs, files);
            } else {
                BreedInputs old = breedInputsDao.findById(breedInputs.getId());
                fileRestService.upload(old, breedInputs, files);
            }
        }
        save(breedInputs);
    }

    @Transactional
    public void save(BreedInputs breedInputs) throws Exception {
        breedInputsDao.save(breedInputs);
        SupplierLog supplierLog = new SupplierLog();
        supplierLog.setSupplierId(breedInputs.getSupplyId());
        supplierLog.setCreateTime(Tools.getCurrentTime());
        supplierLog.setInputsName(breedInputs.getInputsName());
        supplierLog.setNumber(breedInputs.getNumber());
        supplierLog.setSpec(breedInputs.getSpec());
        supplierLog.setUnits(breedInputs.getUnits());
        supplierLog.setSpec(breedInputs.getSpec() + breedInputs.getSpecUnits());
        supplierLogDao.save(supplierLog);
    }

    public void save2(BreedInputs breedInputs) throws Exception {
        save(breedInputs);
    }




    public BreedInputs findById(String id) {
        return breedInputsDao.findById(id);
    }

    public List<String> findByAll(String companyId) {
        return breedInputsDao.findByAll(companyId);
    }

    public List<BreedInputs> findByInputsName(String inputsName, String companyId) {
        return breedInputsDao.findByInputsName(inputsName, companyId);
    }

    public void delete(String id) {
        breedInputsDao.deleteById(id);
    }

    public List<String> findByType(String type, String companyId) {
        return breedInputsDao.findByType(type, companyId);
    }

    public List<BreedInputs> findInputsByType(String type, String companyId) {
        return breedInputsDao.findInputsByType(type, companyId);
    }
}
